package com.resolution.dietaryassessment.view.mydietary.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.example.rxbus.RxBus;
import com.example.rxbus.bean.StatusEvent;
import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.view.mydietary.bean.FoodInfoBean;
import com.resolution.dietaryassessment.view.mydietary.customview.CustomDialog;

import java.util.List;

import es.dmoral.toasty.Toasty;

import static com.resolution.dietaryassessment.view.mydietary.constant.ConstantBean.BREAKFAST_STATUS;
import static com.resolution.dietaryassessment.view.mydietary.constant.ConstantBean.DINNER_STATUS;
import static com.resolution.dietaryassessment.view.mydietary.constant.ConstantBean.LUNCH_STATUS;


/**
 * @author By Resolusion
 * @Title: table容器Recycler
 * @Date: 2020/4/15
 * @Time: 21:48
 * @email Res01ution@foxmail.com
 */
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    private List<FoodInfoBean> mFoodBeans;

    private int state;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View mFoodCardView;
        TextView food_tx;
        ImageView imageView;

        ViewHolder(View view) {
            super(view);
            mFoodCardView = view;
            imageView = view.findViewById(R.id.food_icon);
            food_tx = view.findViewById(R.id.food_name_tx);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.foodcard_item, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("CheckResult")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String food_name = mFoodBeans.get(position).getName();
        int resId = mFoodBeans.get(position).getId();
        holder.imageView.setImageResource(resId);
        holder.food_tx.setText(food_name);
        RxBus.getInstance().toObservable((LifecycleOwner) holder.itemView.getContext(), StatusEvent.class, Lifecycle.Event.ON_ANY).subscribe(statusEvent -> {
            state = statusEvent.getCode();
            holder.imageView.setOnClickListener(v -> {
                if (state == BREAKFAST_STATUS || state == LUNCH_STATUS || state == DINNER_STATUS) {
                    FoodInfoBean foodInfoBean = mFoodBeans.get(holder.getLayoutPosition());
                    showDialog(v.getContext(), foodInfoBean, state);
                }
            });
        });
        holder.imageView.setOnClickListener(v -> {
            if (state == BREAKFAST_STATUS || state == LUNCH_STATUS || state == DINNER_STATUS) {
                FoodInfoBean foodInfoBean = mFoodBeans.get(holder.getLayoutPosition());
                showDialog(v.getContext(), foodInfoBean, state);
            }
        });
        if (state != BREAKFAST_STATUS && state != LUNCH_STATUS && state != DINNER_STATUS) {
            holder.imageView.setOnClickListener(v ->
                    Toasty.error(holder.itemView.getContext(), "请先选择餐次").show());
        }
    }

    @Override
    public int getItemCount() {
        return mFoodBeans.size();
    }

    private void showDialog(Context context, FoodInfoBean foodInfoBean, int state) {
        CustomDialog dialog = new CustomDialog(context, foodInfoBean, state);
        dialog.show();
    }

    public void setData(List<FoodInfoBean> beans) {
        mFoodBeans = beans;
        notifyDataSetChanged();
    }
}
